package org.jeecg.modules.demo.ch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.demo.ch.common.CommonMst;
import org.jeecg.modules.demo.ch.entity.ChHardwareMaintenancePlanDetail;
import org.jeecg.modules.demo.ch.entity.ChMaintenancePlanDetail;
import org.jeecg.modules.demo.ch.entity.ChParameter;
import org.jeecg.modules.demo.ch.entity.ChSoftwareOperationPlan;
import org.jeecg.modules.demo.ch.mapper.ChHardwareMaintenancePlanDetailMapper;
import org.jeecg.modules.demo.ch.mapper.ChMaintenancePlanDetailMapper;
import org.jeecg.modules.demo.ch.mapper.ChParameterMapper;
import org.jeecg.modules.demo.ch.mapper.ChSoftwareOperationPlanMapper;
import org.jeecg.modules.demo.ch.service.IChSoftwareOperationPlanService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 软件运维计划表
 * @Author: jeecg-boot
 * @Date:   2022-10-28
 * @Version: V1.0
 */
@Service
public class ChSoftwareOperationPlanServiceImpl extends ServiceImpl<ChSoftwareOperationPlanMapper, ChSoftwareOperationPlan> implements IChSoftwareOperationPlanService {

	@Resource
	private ChSoftwareOperationPlanMapper chSoftwareOperationPlanMapper;
	@Resource
	private ChMaintenancePlanDetailMapper chMaintenancePlanDetailMapper;
	@Resource
	private ChHardwareMaintenancePlanDetailMapper chHardwareMaintenancePlanDetailMapper;
	@Resource
	private ChParameterMapper chParameterMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(ChSoftwareOperationPlan chSoftwareOperationPlan, List<ChMaintenancePlanDetail> chMaintenancePlanDetailList,List<ChHardwareMaintenancePlanDetail> chHardwareMaintenancePlanDetailList) {
		//生成项目编号  格式 年月日+uuid(前三位)
//		String itemName = geneProNum(chSoftwareOperationPlan);
//		chSoftwareOperationPlan.setItemNo(itemName);
		chSoftwareOperationPlanMapper.insert(chSoftwareOperationPlan);
		if(chMaintenancePlanDetailList!=null && chMaintenancePlanDetailList.size()>0) {
			for(ChMaintenancePlanDetail entity:chMaintenancePlanDetailList) {
				//外键设置
				entity.setChMaintenancePlanId(chSoftwareOperationPlan.getId());
				chMaintenancePlanDetailMapper.insert(entity);
			}
		}
		if(chHardwareMaintenancePlanDetailList!=null && chHardwareMaintenancePlanDetailList.size()>0) {
			for(ChHardwareMaintenancePlanDetail entity:chHardwareMaintenancePlanDetailList) {
				//外键设置
				entity.setChMaintenancePlanId(chSoftwareOperationPlan.getId());
				chHardwareMaintenancePlanDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(ChSoftwareOperationPlan chSoftwareOperationPlan,List<ChMaintenancePlanDetail> chMaintenancePlanDetailList,List<ChHardwareMaintenancePlanDetail> chHardwareMaintenancePlanDetailList) {
		chSoftwareOperationPlanMapper.updateById(chSoftwareOperationPlan);

		//1.先删除子表数据
		chMaintenancePlanDetailMapper.deleteByMainId(chSoftwareOperationPlan.getId());
		chHardwareMaintenancePlanDetailMapper.deleteByMainId(chSoftwareOperationPlan.getId());

		//2.子表数据重新插入
		if(chMaintenancePlanDetailList!=null && chMaintenancePlanDetailList.size()>0) {
			for(ChMaintenancePlanDetail entity:chMaintenancePlanDetailList) {
				//外键设置
				entity.setChMaintenancePlanId(chSoftwareOperationPlan.getId());
				chMaintenancePlanDetailMapper.insert(entity);
			}
		}
		if(chHardwareMaintenancePlanDetailList!=null && chHardwareMaintenancePlanDetailList.size()>0) {
			for(ChHardwareMaintenancePlanDetail entity:chHardwareMaintenancePlanDetailList) {
				//外键设置
				entity.setChMaintenancePlanId(chSoftwareOperationPlan.getId());
				chHardwareMaintenancePlanDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		chMaintenancePlanDetailMapper.deleteByMainId(id);
		chHardwareMaintenancePlanDetailMapper.deleteByMainId(id);
		chSoftwareOperationPlanMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			chMaintenancePlanDetailMapper.deleteByMainId(id.toString());
			chHardwareMaintenancePlanDetailMapper.deleteByMainId(id.toString());
			chSoftwareOperationPlanMapper.deleteById(id);
		}
	}

	@Override
	public int selectChSoftwareOperationPlanListTotalByTime( String authorizationDeadlineBegin, String authorizationDeadlineEnd,String hardwareAuthorizationDeadlineBegin,String hardwareAuthorizationDeadlineEnd,String hardExpirBeginTime,String hardExpirEndTime,String softExpirBeginTime,String softExpirEndTime) {
		return chSoftwareOperationPlanMapper.selectChSoftwareOperationPlanListTotalByTime( authorizationDeadlineBegin, authorizationDeadlineEnd, hardwareAuthorizationDeadlineBegin, hardwareAuthorizationDeadlineEnd, hardExpirBeginTime, hardExpirEndTime, softExpirBeginTime, softExpirEndTime);
	}

	@Override
	public List<ChSoftwareOperationPlan> selectChSoftwareOperationPlanListByTime(Integer pageNo, Integer pageSize, String authorizationDeadlineBegin, String authorizationDeadlineEnd,String hardwareAuthorizationDeadlineBegin,String hardwareAuthorizationDeadlineEnd,String hardExpirBeginTime,String hardExpirEndTime,String softExpirBeginTime,String softExpirEndTime) {
		pageNo = (pageNo-1)*pageSize;
		return chSoftwareOperationPlanMapper.selectChSoftwareOperationPlanListByTime(pageNo, pageSize, authorizationDeadlineBegin,  authorizationDeadlineEnd, hardwareAuthorizationDeadlineBegin, hardwareAuthorizationDeadlineEnd, hardExpirBeginTime, hardExpirEndTime, softExpirBeginTime, softExpirEndTime);
	}

	/**
	 * 生成项目编号
	 * @param chSoftwareOperationPlan
	 */
	public String geneProNum(ChSoftwareOperationPlan chSoftwareOperationPlan){
		LocalDate date = LocalDate.now();
		DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMdd");
		String dateStr = date.format(fmt);
		LambdaQueryWrapper<ChParameter> chParameterLambdaQueryWrapper = new QueryWrapper<ChParameter>().lambda();
		chParameterLambdaQueryWrapper.eq(ChParameter::getMajorCategories, CommonMst.SOFTWARE.SOFTWARE);
		chParameterLambdaQueryWrapper.eq(ChParameter::getLittleClass,CommonMst.SOFTWARE.DESIGN_BOM_FLOW);
		List<ChParameter> chParameters = chParameterMapper.selectList(chParameterLambdaQueryWrapper);
		String valueStr = chParameters.get(0).getValueStr();
		String newValueStr =String.valueOf(Integer.valueOf(valueStr)+1);
		while (valueStr.length()!=4){
			valueStr=CommonMst.SOFTWARE.ZERO+valueStr;
		}
		//更新流水号 +1
		updateValueStr(newValueStr);
		return dateStr+valueStr;
	}

	/***
	 * 更新流水号 +1
	 */
	public void updateValueStr(String newValueStr){
		LambdaUpdateWrapper<ChParameter> chParameterLambdaUpdateWrapper = new UpdateWrapper<ChParameter>().lambda();
		ChParameter chParameter = new ChParameter();
		chParameter.setValueStr(newValueStr);
		chParameterLambdaUpdateWrapper.eq(ChParameter::getMajorCategories, CommonMst.SOFTWARE.SOFTWARE);
		chParameterLambdaUpdateWrapper.eq(ChParameter::getLittleClass,CommonMst.SOFTWARE.DESIGN_BOM_FLOW);
		chParameterMapper.update(chParameter,chParameterLambdaUpdateWrapper);
	}
}
